package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.ChanPinXiLieDaoImpl;
import com.ysd.entity.ChanPinXiLie;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminProductCategoryServlet extends BaseServlet {
	/**
	 * 查询所有产品分类
	 * localhost:8080/qiuxieshangcheng/adminProductCategory?reqName=delProductCate
	 * 
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProductCates(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ChanPinXiLieDaoImpl pcDao = new ChanPinXiLieDaoImpl();// 创建实现类对象
		List<ChanPinXiLie> pcList = pcDao.getChanPinXiLieList();// 查到的所有产品分类集合

		// 发送ajax请求，服务器基本会返回json格式字符串

		response.setCharacterEncoding("UTF-8");// 设置响应的编码格式
		PrintWriter out = response.getWriter();

		Gson gson = new Gson();
		String res = gson.toJson(pcList);

		out.print(res);

		out.flush();
		out.close();

	}

	public void delProductCate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int cId = Integer.parseInt(request.getParameter("categoryId"));
		ChanPinXiLieDaoImpl pcDao = new ChanPinXiLieDaoImpl();
		int n = pcDao.deleteProductCateById(cId);// 调用删除的方法

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		if (n > 0) {// 删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {// 删除失败
			out.print(Result.toClient(false, "删除失败", ""));
		}

		out.flush();
		out.close();

	}

	/**
	 * 添加产品分类
	 * localhost:8080/qiuxieshangcheng/adminProductCategory?reqName=addProductCate
	 * 
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addProductCate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			
			ChanPinXiLieDaoImpl pcDao=new ChanPinXiLieDaoImpl();
			
			String cateName = request.getParameter("cateName"); 
			if (cateName!=null&& !cateName.equals("")) {
				List<ChanPinXiLie> pcList=	pcDao.getChanPinXiLieByName(cateName);
				if (pcList.size()==0) {
					ChanPinXiLie cpxl=new ChanPinXiLie(0, cateName);
					int n=  pcDao.addChanPinXiLie(cpxl);
					if (n>0) {
						out.print(Result.toClient(true,"添加成功",""));
					}else {
						out.print(Result.toClient(false,"添加失败",""));
					}
				} else {
					out.print(Result.toClient(false,"分类名已存在",""));
				}
				
			}	
			out.flush();
			out.close();	
	}
	/**
	 * 
	 * localhost:8080/qiuxieshangcheng/adminProductCategory?reqName=getCateById
	 * 
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getCateById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			
			ChanPinXiLieDaoImpl pcDao=new ChanPinXiLieDaoImpl();
			int Id = Integer.parseInt(request.getParameter("cId"));
			ChanPinXiLie cp =pcDao.getCPById(Id);
			
			Gson gson = new Gson();
			String res = gson.toJson(cp);
			
			out.print(res);
			
			
			out.flush();
			out.close();	
	}
	
	/**
	 * 
	 * localhost:8080/qiuxieshangcheng/adminProductCategory?reqName=updateProCate
	 * 
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateProCate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			
			ChanPinXiLieDaoImpl pcDao=new ChanPinXiLieDaoImpl();
			
			int chanpinId = Integer.parseInt(request.getParameter("cpId"));
			String chanPinXiLie=request.getParameter("cpName");
			
			List<ChanPinXiLie>  pcList =pcDao.getChanPinXiLieByName(chanPinXiLie);
			if (pcList.size()==0) {//没有内容  可以用
				ChanPinXiLie pc = new ChanPinXiLie(chanpinId, chanPinXiLie);
				int n = pcDao.updateCPXL(pc);
				if (n>0) {
					out.print(Result.toClient(true, "修改成功",""));
				} else {
					out.print(Result.toClient(false, "修改失败",""));
				}
			} else {//不能用
				out.print(Result.toClient(false, "已存在",""));
			}
			
			
			
			
			
			
			
			
			
			out.flush();
			out.close();	
	}
}
